/*
 * @lc app=leetcode.cn id=227 lang=cpp
 *
 * [227] 基本计算器 II
 */

// @lc code=start
class Solution
{
public:
  int calculate(string s)
  {
    vector<int> stk;
    char preSign = '+';
    int num = 0;
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      if (isdigit(s[i])) {
        num = num * 10 + int(s[i] - '0');
      }
      if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) {
        switch (preSign) {
          case '+':
            stk.push_back(num);
            break;
          case '-':
            stk.push_back(-num);
            break;
          case '*':
            stk.back() *= num;
            break;
          default:
            stk.back() /= num;
        }
        preSign = s[i];
        num = 0;
      }
    }
    return accumulate(stk.begin(), stk.end(), 0);
  }
};
// @lc code=end
